<?php
namespace src\logic;
use \classlib\core as C;
class lmypros {
	const SPEAKERID = 12;
	/**
	 * 完成扣款且完成信息更新
	 * @param unknown $userid
	 * @param unknown $anchorid
	 * @param unknown $coin
	 * @param unknown $bean
	 * @param unknown $nums
	 * @param unknown $starlv
	 * @param unknown $sid
	 * @param unknown $eventid
	 * @param unknown $giftid
	 * @param unknown $reftype
	 */
	public static function moneyexp($userid, $anchorid, $coin, $bean, $nums, $starlv, $sid, $eventid, $giftid, $reftype) {
		$ip   = C\get_ip();
		$rexp = $coin * 10 + $bean;
		$aexp = $coin * 5 + $bean;
		$earn = 0;
		if ($starlv > 0) {
			$star = C\model::load('main.inc.star')->get_one($starlv);
			$earn = ($star)? $coin * $star['ratio']:0;
		}
		$orm  = C\model::load('main.pl.userinfo')->get_orm();
		$sql  = sprintf('call sendGift(%d, %d, %d, %d, %d, "%s", %d, %d, %d, %d, %f, %d, %d, @result);',
			$userid, $anchorid, $sid, $eventid, $reftype, $ip, $giftid, $nums, $coin, $bean, $earn, $rexp, $aexp);
		$orm->get_parser()->execute($sql);
		$result = $orm->get_value('select @result');
		if ($result) {//清理缓存数据信息
			C\model::load('main.pl.userinfo')->set_dev($userid)->idclear($userid);
			C\model::load('main.pl.userinfo')->set_dev($anchorid)->idclear($anchorid);
			C\model::load('main.pl.finance')->set_dev($userid)->idclear($userid);
			C\model::load('main.pl.finance')->set_dev($anchorid)->idclear($anchorid);
			C\model::load('main.pl.studio')->idclear($sid);
		}
		return $result;
	}
	
	/**
	 * 购买之后记录道具资料信息
	 * @param int $userid  道具的所有者
	 * @param int $reftype 1-坐骑 2-守护 3-礼物道具 4-VIP
	 * @param int $refid   关联购买物品id信息
	 * @param int $airtime 道具的有效时间
	 * @param number $nums 道具购买数量
	 * @param number $stid 道具关联的直播间id编号
	 * @return number[]|unknown[]|string[][]
	 */
	public static function buy($userid, $reftype, $refid, $airtime, $nums=1, $stid = 0) {
		$orm    = C\model::load('main.pl.props')->set_mod($userid);
		$res    = array('id'=>0, 'expire'=>0);
		$where  = array('userid'=>$userid, 'reftype'=>$reftype, 'refid'=>$refid, 'stid'=>$stid);
		$pros   = $orm->get_item($where);
		if (empty($pros)) {
			$expire = ($airtime > 0)? C\config::$nowtime + $airtime:0;
			$fields = array_merge($where, array('expire'=>$expire, 'nums'=>$nums, 'status'=>1, 'stime'=>C\config::$nowtime));
			$res['id']     = $orm->new_one($fields);
			$res['expire'] = $expire;
		} else {
			$expire = ($pros['expire'] < C\config::$nowtime)? (C\config::$nowtime + $airtime):($pros['expire'] + $airtime);
			$fields = array(array('name'=>'nums', 'value'=>'`nums`+'.$nums, 'type'=>\classlib\orm::DT_SQL),
				'expire'=>$expire, 'status'=>1, 'stime'=>C\config::$nowtime);
			$orm->save($pros['id'], $fields);
			$res['expire']      = $fields['expire'];
			$res['id']          = $pros['id'];
		}
		return $res;
	}
	
	/**
	 * 购买喇叭资料信息
	 * @param unknown $userid
	 * @param unknown $nums
	 */
	public static function dospeaker($userid, $nums) {
		$orm = C\model::load('main.pl.props')->set_mod($userid);
		$sql = 'INSERT INTO '.$orm->get_table().'(id, userid, reftype, refid, stid, nums, expire, status, stime)'.
			' VALUE (0, %d, %d, %d, %d, %d, %d, 1, %d) ON DUPLICATE KEY UPDATE nums=nums+%d';
		$sql = sprintf($sql, $userid, 3, self::SPEAKERID, 0, $nums, 0, C\config::$nowtime, $nums);
		$orm->get_orm()->get_parser()->execute($sql);
		$orm->set_ver();
	}
	
	/**
	 * 获取喇叭的礼物道具
	 * @param unknown $userid
	 * @return number
	 */
	public static function getspeaker($userid) {
		$orm    = C\model::load('main.pl.props')->set_mod($userid);
		$where  = array('userid'=>$userid, 'reftype'=>3, 'refid'=>self::SPEAKERID);
		$total  = (int)$orm->get_value('nums', $where);
		return $total;
	}
}